import json
from enum import Enum
from PyQt6.QtGui import QColor


class Role(Enum):
    Background = 'bg'
    Foreground = 'fg'


Background = 'background'
Foreground = 'foreground'
Selected = 'selected'
Cursor = 'cursor'

Black = "black"
Red = "red"
Green = "green"
Brown = "brown"
Blue = "blue"
Magenta = "magenta"
Cyan = "cyan"
White = "white"

BrightBlack = 'brightblack'
BrightRed = "brightred"
BrightGreen = "brightgreen"
BrightBrown = "brightbrown"
BrightBlue = "brightblue"
BrightMagenta = "brightmagenta"
BrightCyan = "brightcyan"
BrightWhite = "brightwhite"

AllColorName = [Background, Foreground, Selected, Cursor,
                Black, Red, Green, Brown,
                Blue, Magenta, Cyan, White,
                BrightBlack, BrightRed, BrightGreen, BrightBrown,
                BrightBlue, BrightMagenta, BrightCyan, BrightWhite]


class Palette(object):

    DEFAULT_PALETTE = f'''
    {{
  "{Background}": "#181818",
  "{Foreground}": "#CCCCCC",
  "{Selected}": "#FFFFFF",
  "{Cursor}": "#FFFFFF",
  
  "{Black}": "#000000",
  "{Red}": "#CD3131",
  "{Green}": "#0DBC79",
  "{Brown}": "#E5E510",
  "{Blue}": "#2472C8",
  "{Magenta}": "#BC3FBC",
  "{Cyan}": "#11A8CD",
  "{White}": "#E5E5E5",

  "{BrightBlack}": "#66666",
  "{BrightRed}": "#F14C4C",
  "{BrightGreen}": "#23D18B",
  "{BrightBrown}": "#F5F543",
  "{BrightBlue}": "#3B8EEA",
  "{BrightMagenta}": "#D670D6",
  "{BrightCyan}": "#29B8DB",
  "{BrightWhite}": "#F5F5F5"
}}
    '''

    def __init__(self, color_config: str = None):
        data = color_config if color_config is not None else Palette.DEFAULT_PALETTE
        p_obj = json.loads(data)
        self.colors = {}

        for k, v in p_obj.items():
            self.colors[k] = QColor.fromString(v)

    def get_color(self, name: str, role: Role = Role.Foreground):
        if name in self.colors:
            return self.colors[name]

        if role == Role.Foreground:
            return self.colors[Foreground]
        elif role == Role.Background:
            return self.colors[Background]



